using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.Mvc;
using Web.Core.Helpers;
using System.Web.Mvc.Html;
using Web.Core.Components;
using System.Collections.Generic;

namespace Web.Core.Extensions
{
    public static class HtmlHelperExtension
    {
        public static AyattaHtmlHelper Ayatta(this HtmlHelper helper)
        {
            return new AyattaHtmlHelper(helper);
        }
        public static MvcForm BeginGetForm(this HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
        {
            var formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
            var tagBuilder = new TagBuilder("form");
            if (htmlAttributes != null)
            {
                tagBuilder.MergeAttributes(htmlAttributes);
            }
            tagBuilder.MergeAttribute("action", formAction);
            tagBuilder.MergeAttribute("method", "get", true);
            htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));

            return new MvcForm(htmlHelper.ViewContext);
        }

        public static IHtmlString Pager<T>(this HtmlHelper htmlHelper, PagedList.IPagedList<T> list)
        {
            return new HtmlString(new Pager<T>(list).ToString());
        }

        public static IHtmlString Control(this HtmlHelper helper,Control control)
        {
            var sb = new StringBuilder();
            using (var writer=new StringWriter(sb))
            using (var htmlWriter = new HtmlTextWriter(writer))
            {
                control.RenderControl(htmlWriter);
            }
            return new HtmlString(sb.ToString());
        }
    }
}